﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CustomConsole;

namespace WpfConsole
{
    /// <summary>
    /// ConsoleView.xaml の相互作用ロジック
    /// </summary>
    public partial class ConsoleView : UserControl, IDisposable, IBookTarget
    {
        public DateTime LastChange { get { return ConsoleControl.Console.LastChange; } }
        public ConsoleView()
        {
            InitializeComponent();
            ConsoleControl.Focus();
            ConsoleControl.ConsoleClose += ()=> { if (ConsoleClose != null) ConsoleClose(this); };

            ConsoleControl.ConsoleChange += (x) => { UpdateScrollBar(); };

            HorizontalScrollBar.Scroll += new System.Windows.Controls.Primitives.ScrollEventHandler(HorizontalScrollBar_Scroll);
            VerticalScrollBar.Scroll += new System.Windows.Controls.Primitives.ScrollEventHandler(VerticalScrollBar_Scroll);

        }

        public void SendCommand(string command)
        {
            ConsoleControl.Console.SendText(command + "\r\n");
        }

        public bool Active
        {
            get { return ConsoleControl.Active; }
            set { ConsoleControl.Active = value; }
        }

        public event Action<ConsoleView> ConsoleClose;

        void VerticalScrollBar_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {
            int deltaRow = (int)e.NewValue - ConsoleControl.Console.VerticalScrollPosition;
            /*
            if (Math.Abs(deltaRow) < ConsoleControl.Console.Rows)
            {
                deltaRow *= ConsoleControl.Console.Rows;
            }
             * */
            ConsoleControl.SetScrollPosition(0, deltaRow);
        }

        void HorizontalScrollBar_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {
            int deltaCol = (int)e.NewValue - ConsoleControl.Console.HorizontalScrollPosition;
            /*
            if (Math.Abs(deltaCol) < ConsoleControl.Console.Columns)
            {
                deltaCol *= ConsoleControl.Console.Columns;
            }
             * */
            ConsoleControl.SetScrollPosition(deltaCol, 0);
        }


        private void UpdateScrollBar()
        {
            IConsoleHost console = ConsoleControl.Console;
            if (console.NeedHorizontalScrollBar)
            {
                HorizontalScrollBar.Visibility = Visibility.Visible;
                HorizontalScrollBar.Maximum = console.BufferColumns - 1;
                HorizontalScrollBar.Value = console.HorizontalScrollPosition;
            }
            else
            {
                // Deadlock when Collapse (SetConsoleSize and ConsoleChange) happen.
                HorizontalScrollBar.Visibility = Visibility.Hidden;
            }

            if (console.NeedVerticalScrollBar)
            {
                VerticalScrollBar.Visibility = Visibility.Visible;
                VerticalScrollBar.Maximum = console.BufferRows - 1;
                VerticalScrollBar.Value = console.VerticalScrollPosition;
            }
            else
            {
                VerticalScrollBar.Visibility = Visibility.Hidden;
            }
        }

        #region IDisposable メンバ

        public void Dispose()
        {
            ConsoleControl.Dispose();
        }

        #endregion
    }
}
